/*
 * HomeMenuLayout.java
 * business
 *
 * Created by ChenTao on 2017/2/21.
 *
 * Copyright (c) 2017年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.ui.widget.view;

import android.content.Context;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yidiandao.business.utils.UIUtils;

/** 首页-协调菜单布局的Layout */
public class HomeMenuLayout extends LinearLayout {

    public HomeMenuLayout(Context context) {
        super(context);
    }

    public HomeMenuLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {

        View firstChild = getChildAt(0);
        View secondChild = getChildAt(1);

        int itemMeasuredHeight = firstChild.getMeasuredHeight();

        if (secondChild.getVisibility() != GONE) {
            // 有两行菜单
            int dividerHeight = (int) ((getMeasuredHeight() - 2 * itemMeasuredHeight) / 3f + 0.5f);

            firstChild.layout(
                    0, dividerHeight, getMeasuredWidth(), dividerHeight + itemMeasuredHeight);
            secondChild.layout(
                    0,
                    dividerHeight * 2 + itemMeasuredHeight,
                    getMeasuredWidth(),
                    itemMeasuredHeight * 2 + dividerHeight * 2);
        } else {
            // 只有一行菜单
            int dividerHeight = (int) ((getMeasuredHeight() - itemMeasuredHeight) * 0.5f + 0.5f);
            firstChild.layout(
                    0, dividerHeight, getMeasuredWidth(), dividerHeight + itemMeasuredHeight);
        }
    }

    private void getFontSize() {
        TextView textView = new TextView(getContext());
        textView.setTextSize(UIUtils.dp2px(14));
        TextPaint paint = textView.getPaint();
        float ascent = paint.ascent();
        float descent = paint.descent();
    }
}
